package com.zz.inventory.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zz.inventory.domain.FranchiseBills;
import com.zz.inventory.service.IFranchiseBillsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 账单Controller
 * 
 * @author zz
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/bills")
public class FranchiseBillsController extends BaseController
{
    @Autowired
    private IFranchiseBillsService franchiseBillsService;


    /**
     * 查询账单列表
     */
    @RequiresPermissions("platform:bills:list")
    @GetMapping("/list")
    public TableDataInfo list(FranchiseBills franchiseBills)
    {
        startPage();
        List<FranchiseBills> list = franchiseBillsService.selectFranchiseBillsList(franchiseBills);
        return getDataTable(list);
    }

    /**
     * 导出账单列表
     */
    @RequiresPermissions("platform:bills:export")
    @Log(title = "账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FranchiseBills franchiseBills)
    {
        List<FranchiseBills> list = franchiseBillsService.selectFranchiseBillsList(franchiseBills);
        ExcelUtil<FranchiseBills> util = new ExcelUtil<FranchiseBills>(FranchiseBills.class);
        util.exportExcel(response, list, "账单数据");
    }

    /**
     * 获取账单详细信息
     */
    @RequiresPermissions("platform:bills:query")
    @GetMapping(value = "/{billId}")
    public AjaxResult getInfo(@PathVariable("billId") Long billId)
    {
        return success(franchiseBillsService.selectFranchiseBillsByBillId(billId));
    }

    /**
     * 新增账单
     */
    @RequiresPermissions("platform:bills:add")
    @Log(title = "账单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FranchiseBills franchiseBills)
    {
        return toAjax(franchiseBillsService.insertFranchiseBills(franchiseBills));
    }

    /**
     * 修改账单
     */
    @RequiresPermissions("platform:bills:edit")
    @Log(title = "账单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FranchiseBills franchiseBills)
    {
        return toAjax(franchiseBillsService.updateFranchiseBills(franchiseBills));
    }

    /**
     * 删除账单
     */
    @RequiresPermissions("platform:bills:remove")
    @Log(title = "账单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{billIds}")
    public AjaxResult remove(@PathVariable Long[] billIds)
    {
        return toAjax(franchiseBillsService.deleteFranchiseBillsByBillIds(billIds));
    }

    @GetMapping("/statistics")
    @CrossOrigin("*")
    public AjaxResult getBillStatistics(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(value = "status", required = false) String status) {

        // 1. 计算汇总数据
        Map<String, Object> summary = franchiseBillsService.getBillSummary(startDate, endDate, status);

        // 2. 获取图表数据
        Map<String, Object> charts = new HashMap<>();
        // 月度收入趋势数据
        charts.put("monthlyTrend", franchiseBillsService.getMonthlyAmountTrend(startDate, endDate, status));
        // 账单状态分布数据
        charts.put("statusDistribution", franchiseBillsService.getBillStatusDistribution(startDate, endDate));
        // 套餐销售排行数据
        charts.put("packageRanking", franchiseBillsService.getPackageRanking(startDate, endDate));
        // 学校消费排行数据
        charts.put("schoolRanking", franchiseBillsService.getSchoolConsumptionRanking(startDate, endDate));

        Map<String, Object> result = new HashMap<>();
        result.put("summary", summary);
        result.put("charts", charts);

        return AjaxResult.success(result);
    }
}
